package 生产者消费者模型;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2022-12-27
 * Time: 11:54
 */

public class Main {
    public static void main(String[] args) {
        MessageQueue queue = new MessageQueue(2);

        //三个生产者来产生消息
        for(int i =0;i<3;++i) {
            int id = i;
            new Thread(()->{
                Message message = new Message(id,"值 : " + id);
                queue.put(new Message(id,"值 : " + id));
            },"生产者" + i).start();
        }

        //使用一个消费者不断消费
        new Thread(()->{
           while(true) {
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               queue.take();
           }
        },"消费者").start();
    }
}